package com.yang.dao;

import com.yang.entity.Orderdetail;

import java.sql.*;
import java.util.*;

public class OrderdetailDao {
    //根据订单编号查询订单详情
    public List<Orderdetail> queryByOrderId(String oid){
        Connection conn= BaseDao.getConn();
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps=conn.prepareStatement("select * from Orderdetail where orderid=?");
            ps.setString(1,oid);
            rs = ps.executeQuery();
            List list=new ArrayList();
            while(rs.next()){
                Orderdetail o=new Orderdetail();
                o.setId(rs.getInt("id"));
                o.setOrderid(rs.getString("Orderid"));
                o.setProductname(rs.getString("Productname"));
                o.setProductid(rs.getInt("Productid"));
                o.setProductcrice(rs.getDouble("Productcrice"));
                o.setProductcount(rs.getInt("Productcount"));
                list.add(o);
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.close(rs,ps,conn);
        }
        return null;
    }
    //根据商品id删除
    public int deleteById(String id){
        Connection conn= BaseDao.getConn();
        PreparedStatement ps=null;
        int sum=0;
        try {
            ps=conn.prepareStatement("delete from orderdetail where orderid=?");
            ps.setString(1,id);
            sum= ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.close(null,ps,conn);
        }
        return sum;
    }
}
